package com.example.demo.controller;

import com.example.demo.pojo.User;
import com.example.demo.service.LoginaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@ComponentScan
public class LoginCheckController {

    @Autowired
    private LoginaService loginaService;

    @Transactional
    @PostMapping("/loginCheck")
    public ModelAndView login(String username, String password, String codeInput, Model model, HttpSession session) {
        String codeSession = (String) session.getAttribute("codeSession");
        //判断验证码
        if (loginaService.checkCode(codeSession, codeInput)) {
            //先判断用户是否存在
            if(loginaService.usernameIfExist(username) != null){
                //先判断userState是否可用
                if(loginaService.loginCheckState(username) == 0){
                    //再判断密码
                    User user = loginaService.loginCheck(username, password);
                    if (user != null) {
                        model.addAttribute("username", user.getUsername());
                        model.addAttribute("loginRole", user.getRole());//可以根据role识别进入用户界面或者管理员页面
                        session.setAttribute("username", user.getUsername());
                        session.setAttribute("loginRole", user.getRole());
                        return new ModelAndView("redirect:/loginSuccess");
                    } else {
                        return new ModelAndView("redirect:/loginFail-password");
                    }
                }else{
                    return new ModelAndView("redirect:/loginFail-userState");
                }
            }else {
                return new ModelAndView("redirect:/loginFail-user");
            }
        } else
            return new ModelAndView("redirect:/codeFalse");
    }
    @RequestMapping("/loginFail-password")
    @ResponseBody
    public String loginFailpassword() {
        return "<script type='text/javascript'>alert('登录失败,密码错误，请重新输入');window.location='/login'</script>";
    }
    @RequestMapping("/loginFail-userState")
    @ResponseBody
    public String loginFailuserState() {
        return "<script type='text/javascript'>alert('登录失败,用户已停用，请重新输入');window.location='/login'</script>";
    }
    @RequestMapping("/loginFail-user")
    @ResponseBody
    public String loginFailuser() {
        return "<script type='text/javascript'>alert('登录失败,用户不存在，请重新输入');window.location='/login'</script>";
    }
    @RequestMapping("/codeFalse")
    @ResponseBody
    public String codeFalse() {
        return "<script type='text/javascript'>alert('登录失败,验证码错误，请重新输入');window.location='/login'</script>";
    }
    @RequestMapping("/login-go")
    @ResponseBody
    public String nologin() {
        return "<script type='text/javascript'>alert('您未进行登录,请进行登录');window.location='/login'</script>";
    }
    @RequestMapping("/login")
    public String loginYemian() {
        return "login";
    }


}
